var app = getApp();
// var server = require('../../utils/server.js');
Page({
    data: {
        currentTab: 0,
        selectedAllStatus: false,
        total:0,
        param:'',
        xjparam:'',
        selected:'',
        loanlist: [],
        historylist:[],
        aid:'',
        reader_id:'',
        password:'',
        renewShow:false,
        xjArray:[],
        yijianxujielist_show:false,
        },



    onLoad: function (options) {
        var that = this;
        
        console.log('onload------>options');
        console.log(options);
        var aid = options.aid;
        var reader_id = wx.getStorageSync(aid + 'user').reader_id;         
        console.log(reader_id);
        if (aid) {
            that.setData({
                aid: aid,
                reader_id: reader_id,
                flag: ''
            })
            console.log('-------options-->aid------');
            console.log(aid);
        };
        wx.request({
            url: 'https://2016.bookgo.com.cn/book/apiapp/get_password',
            data: {
                aid:aid,
                reader_id:reader_id
            },
            success: function(res) {
                console.log('book/apiapp/get_password---->success---->res');
                console.log(res);
                if(res.data.code == 101){
                    that.setData({
                        password:res.data.msg.password
                    })
                }
                wx.showLoading({
                    title: '请稍等',
                })
                var t = setTimeout(
                    function () {
                        wx.hideLoading()
                    },
                    10000)

                that.loadData();
        // clearTimeout(t);
            }
        });   
    },

    loadData: function () {
        var that = this;
        var total = that.data.total;
        var password = that.data.password;
        console.log('---------------password----------------');
        console.log(password);
        wx.request({
            url: 'https://2016.bookgo.com.cn/book/apiapp/getLoanInfo',
            data: {
                reader_id: that.data.reader_id,
                password: that.data.password,
                aid: that.data.aid
            },
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                console.log('getLoanInfo----->success');
                console.log(res);
                for (var i = 0; i < res.data.loanlist.length; i++) {
                    res.data.loanlist[i].num = 1;
                    res.data.loanlist[i].selected = false;
                }
                // console.log(res.data.loanlist);
                that.setData({
                    total:0,
                    loanlist: res.data.loanlist,
                    historylist: res.data.historylist
                })
                wx.hideLoading();
            }

        })

    },
    // 滑动切换tab 
    bindChange: function (e) {
        console.log('------------')
        console.log(e);
        var that = this;
        that.setData({ 
                currentTab: e.detail.current 
            });
    },
    // 点击tab切换 
    swichNav: function (e) {
        console.log(e);
        var that = this;
        if (this.data.currentTab === e.target.dataset.current) {
            return false;
        } else {
            that.setData({
                currentTab: e.target.dataset.current
            })
        }
    },

    //勾选事件处理
    bindCheckbox: function (e) {
        // console.log('------------bindCheckbox--->e--------------');
        // console.log(e);
        var that = this;
        var index = parseInt(e.currentTarget.dataset.index);
        that.data.loanlist[index].selected = !that.data.loanlist[index].selected;
        // console.log(that.data.loanlist);
        var xjparam = e.currentTarget.dataset.xjparam;
        if (e.currentTarget.dataset.selected == true){
            var params = '';
            params = xjparam + ',';
            that.data.param += params;
            // console.log('-----------param begin---------');
            // console.log(that.data.param);
            // console.log('-----------param end---------');
            that.setData({
                param:that.data.param,
            })
        }else{
            var xjparam = e.currentTarget.dataset.xjparam;
            // console.log(xjparam);
            that.data.param = that.data.param.replace(xjparam+',','');
            // console.log(that.data.param);            
        }
        
        var totalAll = 0;        
        //计算书本数量
        if (that.data.loanlist[index].selected){
            that.data.total = that.data.total + that.data.loanlist[index].num;
        }else{
            that.data.total = that.data.total - that.data.loanlist[index].num;
        }
        //判断是否全选
        for (var i = 0; i < that.data.loanlist.length; i++){
            totalAll = totalAll + that.data.loanlist[i].num;
        }
        if (totalAll == that.data.total){
            that.data.selectedAllStatus = true;
        }else{
            that.data.selectedAllStatus = false;
        }
            that.setData({
                selectedAllStatus: that.data.selectedAllStatus,
                total:that.data.total,
                loanlist: that.data.loanlist,
                xjparam: xjparam,

            })  
    },

    //全选
    selectedAll: function (e) {
        // console.log('-------------bindtap=selectedAll-------------');
        // console.log(e);
        var that = this;
        var param = '';//防止 单选几个--->全选---->一键，xjparam = 单选的几个 + 全选的xjparam，先把单选的xjparam清空
        if (!that.data.selectedAllStatus){
            that.data.total = that.data.loanlist.length;
            for (var i = 0; i < that.data.loanlist.length; i++){
                that.data.loanlist[i].selected = true;
                // that.data.total = that.data.total + that.data.loanlist[i].num;
                param += that.data.loanlist[i].xjparam + ',';
            }
        }else{
            for (var i = 0; i < that.data.loanlist.length; i++) {
                that.data.loanlist[i].selected = false;
            }
            that.data.total = 0;
        }
        that.setData({
            selectedAllStatus: !that.data.selectedAllStatus,
            loanlist: that.data.loanlist,
            total:that.data.total,
            param:param
        })
    },

    //单本续借
    renew: function (e) {
        var that = this;
        var xjparam = e.currentTarget.dataset.xjparam;
        if (xjparam){
            var bookname = e.currentTarget.dataset.bookname;
            var param = xjparam + ',';
            console.log('----------password-------------');
            console.log(that.data.password);
            wx.request({
                url: 'https://2016.bookgo.com.cn/book/apiapp/renew',
                data: {
                    aid: that.data.aid,
                    reader_id: that.data.reader_id,
                    password: that.data.password,
                    param: param
                },
                header: {
                    'content-type': 'application/json'
                },
                success: function (res) {
                    // console.log(res);
                    if (res.data[0].flag == 1) {
                        wx.showModal({
                            title: '提示',
                            content: '<<' + bookname + '>>' + '续借成功',
                            showCancel:false,
                            success: function (res) {
                                if (res.confirm) {
                                    console.log('用户点击确定');
                                    that.setData({
                                        param:''
                                    })                                   
                                    that.loadData();   
                                }
                            }
                        })
                    } else if (res.data[0].flag == 0) {
                        wx.showModal({
                            title: '提示',
                            content: '<<' + bookname + '>>' + '已续借过一次',
                            showCancel: false,
                            success: function (res) {
                                if (res.confirm) {
                                    console.log('用户点击确定');
                                    that.setData({
                                        param: ''
                                    })
                                    //刷新页面
                                    that.loadData();   
                                } 
                            }
                        })
                    }
                }
            })
        }else{
            console.log('失败');
        }       
    },

    //一键续借
    yijian_renew:function () {
        var that = this;
        wx.showLoading({
            title: '请稍等',
        })
        setTimeout(function () {
            wx.hideLoading()
        }, 2000)
        that.yijian();
    },
    yijian: function (e) {
        // console.log(e)
        var that = this;
        that.data.xjArray = [];
        var param = that.data.param;
        if (param){
            wx.request({
                url: 'https://2016.bookgo.com.cn/book/apiapp/renew',
                data: {
                    aid: that.data.aid,
                    reader_id: that.data.reader_id,
                    password: that.data.password,
                    param: param
                },
                header: {
                    'content-type': 'application/json'
                },
                success: function (res) {
                    // console.log(res.data);
                    var xjlist = res.data,
                        xjArray = that.data.xjArray,
                        loanlist = that.data.loanlist;
                    for (var i = 0; i < xjlist.length; i++) {
                        var xjObject = null;
                        xjObject = Object.assign(xjlist[i], loanlist[i]);
                        xjArray[i] = xjObject;
                    }
                    that.setData({
                        xjArray: xjArray,
                        yijianxujielist_show: true,
                        param:''     //刷新param，避免再次点击时累积                       
                    })
                    // console.log('----------------xjArray--------------');
                    // console.log(xjArray);
                    //刷新页面
                    that.loadData();
                }
            })
        }else{
            wx.showModal({
                title: '提示',
                content: '请选择图书',
                showCancel: false,
                success: function (res) {
                    if (res.confirm) {
                        console.log('用户点击确定');
                    }
                }
            })
        }
        
    },

    //关闭一键续借结果弹窗
    close_yijianxujielist_show: function (e) {
        // console.log('close_yijianxujielist_show--->e');
        // console.log(e);
        var that = this;
        that.setData({
            yijianxujielist_show: false,
            // selected:false,
            selectedAllStatus:false,
        })        
    },

    //评论
    review: function (e) {
        // console.log(e);
        var that = this;
        var isbn = e.currentTarget.dataset.isbn,
            reader_id = that.data.reader_id,
            password = that.data.password,
            aid = that.data.aid;
        wx.navigateTo({
            url: 'review/review?aid=' + aid + '&isbn=' + isbn,
        })
    },



    /**
 * 生命周期函数--监听页面初次渲染完成
 */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }    
}) 